﻿// JScript 文件
$(document).ready(function(){
    //获得问卷
    var qnid = $("#qnid").val();
    $.ajax({
        type:"GET",
        url:"Comsrv/Comsrv.aspx",
        dataType:"json",
        data:"oper=GetQN_Cache&qnid="+qnid,
        success:function(data){
            ArrayForm = data[0].Questions;
            if ( ArrayForm.length>0 )
            {
                $("#SurveyForm").empty();
                var html ="";
                for (var i=0;i<ArrayForm.length;i++)
                {
                    html+="<ul>"+ArrayForm[i].ORDERIDX+"."+ArrayForm[i].QUESTION1+"</ul><ul>";
                    switch ( ArrayForm[i].TYPE )
                    {
                        case "single":
                            html+=FormatSingle(ArrayForm[i]);
                        break;
                        
                        case "single_other":
                            html+=FormatSingleOther(ArrayForm[i]);
                        break;
                        
                        case "multicheck":
                            html+=FormatMulticheck(ArrayForm[i]);
                        break;
                        
                        case "multicheck_other":
                            html+=FormatMulticheckOther(ArrayForm[i]);
                        break;
                        
                        case "singleblank":
                            html+=FormatSingleBlank(ArrayForm[i]);
                        break;
                    }
                    
                    
                    html+="</ul>";
                    
                }
                $("#SurveyForm").html(html);
            }
        }
 
        
    })
});




//提交问卷
function SubmitForm()
{
        //注意：dtoAccount是JS的数据模型，必须和Server端的数据模型相同
        var Account = new dtoAccount(); 
        Account.Id = 0;
		Account.COMPANY = $("#txtCompanyName").val();
		Account.ADDRESS = $("#txtAddress").val();
		Account.ZIP = "";
		Account.FULLNAME = $("#txtAccountName").val();
		Account.GENDER = "";
		Account.JOB = "";
		Account.TEL = $("#txtPhone").val();
		Account.MOBILE = "";
		Account.FAX = "";
		Account.EMAIL = "";
		Account.REMARKS = "";
		Account.CREATEDATE = new Date();
		Account.STATUS = 0;
		Account.CITY = "";
		Account.PROVINCE = "";
		Account.PVFROM = "";
		Account.Questionnaireid = $("#qnid").val();
        
	   
        //格式化为Json集合
        var jsonText = JSON.stringify(Account)
        //上传数据
        $.ajax({
          type: "POST",
          url: "./Comsrv/Comsrv.aspx?oper=PutAccount",
          data: jsonText,
          success: function(msg){
                 if (isNumber(msg))
                 {
                    PutAnswer(msg);
                 }
                 else
                 {
                    alert("对不起注册失败,请重新尝试！");     
                      
                 }
           }
        });
}
function PutAnswer(CurrUid)
{
    var ArrayAnswer = new Array();//定义问卷答案数组
    //处理多选
    $("input[@type=checkbox]").each(function(){
        if($(this).attr("checked"))
        {
            
            var answer = new dtoAnswer();
            var arrCK = $(this).attr("ID").split("_")
            answer.QUESTIONNAIREID  = $("#qnid").val();
            answer.QUESTIONID       = arrCK[1];
            answer.OPTIONID         = arrCK[2];
            answer.VAL              = $(this).attr("value");
            answer.LABEL            = $(this).attr("value");
            answer.ACCOUNTID        = CurrUid;
            answer.ANSWERTEXT       = (answer.VAL=="其他" ? $("#Option_"+arrCK[2]+"_text").val():answer.VAL );
            ArrayAnswer.push(answer);
        }
    })
   //处理单选
   $("input[@type=radio]").each(function(){
        if($(this).attr("checked"))
        {
            var answer = new dtoAnswer();
            var arrCK = $(this).attr("ID").split("_")
            answer.QUESTIONNAIREID  = $("#qnid").val();
            answer.QUESTIONID       = arrCK[1];
            answer.OPTIONID         = arrCK[2];
            answer.VAL              = $(this).attr("value");
            answer.LABEL            = $(this).attr("value");
            answer.ACCOUNTID        = CurrUid;
            answer.ANSWERTEXT       = (answer.VAL=="其他" ? $("#Option_"+arrCK[2]+"_text").val():answer.VAL );
            ArrayAnswer.push(answer); 
        }
        
   });
   
   //处理问答
   $("textarea[@name=text]").each(function(){

            var answer = new dtoAnswer();
            var arrCK = $(this).attr("ID").split("_")
            answer.QUESTIONNAIREID  = $("#qnid").val();
            answer.QUESTIONID       = arrCK[1];
            answer.OPTIONID         = arrCK[2];
            answer.VAL              = "text";
            answer.LABEL            = "text";
            answer.ACCOUNTID        = CurrUid;
            answer.ANSWERTEXT       = $(this).val();
            ArrayAnswer.push(answer); 
        
   });
   
    var jsonText = JSON.stringify(ArrayAnswer);
    //上传数据
        $.ajax({
          type: "POST",
          url: "./Comsrv/Comsrv.aspx?oper=PutAnswer",
          data:"html",
          data: jsonText,
          success: function(msg){
                 if (msg=="true")
                 {
                    alert("成功啦！");
                 }
                 else
                 {
                    alert("对不起添加失败,请重新尝试！");     
                      
                 }
           }
        });
    
}

/*** 检查是否为数 ***/
function isNumber (o) {
    return (o.search(/^[+-]?[0-9.]*$/) >= 0);
}